﻿@page "/CascadingAuthenticationStateConsumer"
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization

<h1>Cascading authentication state</h1>

@if (user == null)
{
    <span id="auth-state-pending">Requesting authentication state...</span>
}
else
{
    <p>
        Authenticated:
        <strong id="identity-authenticated">@user.Identity.IsAuthenticated</strong>
    </p>

    <p>
        Name:
        <strong id="identity-name">@user.Identity.Name</strong>
    </p>

    <p>
        Test claim:
        @if (user.HasClaim(TestClaimPredicate) == true)
        {
            <strong id="test-claim">@user.Claims.Single(c => TestClaimPredicate(c)).Value</strong>
        }
        else
        {
            <strong id="test-claim">(none)</strong>
        }
    </p>
}

@code
{
    static Predicate<Claim> TestClaimPredicate = c => c.Type == "test-claim";

    ClaimsPrincipal user;

    [CascadingParameter] Task<AuthenticationState> AuthenticationStateTask { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        user = (await AuthenticationStateTask).User;
    }
}
